package com.shaozi.nc.quartz.attendance;

import org.apache.log4j.Logger;
import org.json.JSONObject;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import com.shaozi.data.mq.business.MQBus;
import com.shaozi.data.mq.business.base.MqConsts;
import com.shaozi.data.mq.business.base.SourceTypeConsts;
import com.shaozi.data.mq.business.bean.MQExchangeData;
import com.shaozi.data.mq.business.bean.MQExchangeRuleBean;
import com.shaozi.data.mq.business.bean.MQPushItem;
import com.shaozi.im.protocol.core.IMConsts;
import com.shaozi.lib.throwable.SzException;
import com.shaozi.nc.data.hibVO.AttendanceVO;
import com.shaozi.nc.hibservice.AttendanceService;
import com.shaozi.nc.mq.bean.attendance.CommonBean;
import com.shaozi.nc.quartz.QuartzJob;
import com.shaozi.utils.common.JsonUtil;
import com.shaozi.utils.common.SysUtils;
import com.shaozi.utils.date.DateHelper;

public class ComplainEndDateQuartzJob implements QuartzJob {

	private Logger log = Logger.getLogger(ComplainEndDateQuartzJob.class);
	@Autowired
	private AttendanceService attendanceService;
	@Autowired
	private MQBus mqBus;

	public ComplainEndDateQuartzJob() {

	}

	@Override
	public void execute(JobExecutionContext context) throws JobExecutionException {
		JobDataMap jobData = context.getJobDetail().getJobDataMap();
		String companyId = jobData.getString("companyId");
		Integer uid = Integer.parseInt(jobData.getString("uid"));
		Integer id = Integer.parseInt(jobData.getString("id"));
		// long startAt =
		// Long.parseLong(jobData.getString(Consts.DataKey_StartAt));

		try {
			AttendanceVO vo = attendanceService.getAttendanceComplains(companyId, id);
			if (vo == null)
				return;
			dispatchVO(companyId, uid, vo);
		} catch (SzException e) {
			log.error(e.getStackMessage());
		}
	}

	private void dispatchVO(String companyId, Integer uid, AttendanceVO vo) throws SzException {
		int appealDay = getAppealDay(vo.getJsonRule());
		if (appealDay == 0)
			return;
		int dateDiff = DateHelper.dateDiff(vo.getStartHandleTime(), System.currentTimeMillis());

		if (dateDiff <= 0)
			return;
		if (dateDiff > appealDay)
			return;
		String uuid = SysUtils.buildUuid("");
		mqBus.decomposeData(genMqExchangeData(companyId, uid, vo, appealDay - dateDiff), "ComplainEndDateQuartzJob",
				true, uuid);
	}

	private String genMqExchangeData(String companyId, Integer uid, AttendanceVO vo, int dateDiff) {
		MQExchangeData<MQExchangeRuleBean<CommonBean>> data = new MQExchangeData<MQExchangeRuleBean<CommonBean>>();
		data.setCompanyId(companyId);
		data.setDataTime(System.currentTimeMillis());
		data.setAction_type(MqConsts.ActionType.Push);
		MQExchangeRuleBean<CommonBean> rule = new MQExchangeRuleBean<CommonBean>();
		MQPushItem pushItem = new MQPushItem();
		pushItem.setAtTime(0l);
		pushItem.setLevelType(MqConsts.LevelType.User);
		pushItem.setSourceType(SourceTypeConsts.OA.Attendance.Complain_End_Date_Alarm);
		pushItem.setUserId(String.valueOf(IMConsts.SysMemberId.Secretary_Id));
		rule.setPush(pushItem);
		CommonBean biz = new CommonBean();
		biz.setUserId(uid);
		biz.setOutDays(dateDiff);
		biz.setHandleTime(vo.getStartHandleTime());
		biz.setId(vo.getId());
		rule.setBiz(biz);
		data.setRule(rule);

		return JsonUtil.toJsonString(data);
	}

	private int getAppealDay(String jsonRule) {
		if (StringUtils.isEmpty(jsonRule))
			return 0;
		JSONObject jsonObj = new JSONObject(jsonRule);
		try {
			return jsonObj.getJSONObject("rule").getInt("appeal_day");
		} catch (Exception e) {
			return 0;
		}
	}
}
